package net.qunzhi.common.utils;

import net.qunzhi.common.exception.RRException;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;

public class FileTypeUtils {

    //Integer fileType文件类型，0：其他文件，1：头像文件，2：图片，3：音频
    private static String[] imageType = {"wepb", "bmp", "gif", "jpg", "jpeg", "exif", "svg", "png", "ico"};

    private static String[] audioType = {"mp3", "wma", "avi", "rm", "rmvb", "flv", "mpg", "mov", "mkv"};

    public static void check(Integer fileType, String suffix){
        if(!judge( fileType,  suffix)){
            throw new RRException("请检查文件类型");
        }
    }

    public static boolean judge(Integer fileType, String suffix) {
        if (null == fileType || StringUtils.isBlank(suffix)) {
            return false;
        }
        //1：头像文件，2：图片
        if (fileType.equals(1) || fileType.equals(2)) {
            List<String> imageTyepLists = Arrays.asList(imageType);
            if (imageTyepLists.contains(suffix.toLowerCase())) {
                return true;
            } else {
                return false;
            }

        }
        if (fileType.equals(3)) {
            List<String> audioTypeLists = Arrays.asList(audioType);
            if (audioTypeLists.contains(suffix.toLowerCase())) {
                return true;
            } else {
                return false;
            }
        }
        return true;
    }

}
